#include "gui.h"
#include "render_engine.h"

#include <CEGUI/CEGUIBase.h>
#include <CEGUI/CEGUISchemeManager.h> 
#include <CEGUI/CEGUIWindowManager.h>
#include <CEGUI/CEGUIImageset.h>
#include <CEGUI/CEGUISystem.h>

#include "ogre/OgreCEGUIRenderer.h"
#include "ogre/OgreCEGUIResourceProvider.h"
#include <Ogre.h>


using namespace Ogre;

namespace Wonderland{

/*
 * CEGUI is used for the gui for now - until chrome is usable on the mac.
 *
 */

Gui::Gui(RenderEngine *renderer){
	window_ = renderer->getWindow();
	scene_manager_ = renderer->getSceneManager();
  
  createScene();
}

// Add the gui layer to the scene
void Gui::createScene(){
  
	mRenderer = new CEGUI::OgreCEGUIRenderer(window_, Ogre::RENDER_QUEUE_OVERLAY, false, 3000, scene_manager_);
	mSystem = new CEGUI::System(mRenderer);

	/* Cargo culted from ogresamples/gui.cpp */
		// Setup Render To Texture for preview window
		TexturePtr rttTex = TextureManager::getSingleton().createManual("RttTex",  ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, TEX_TYPE_2D,  512, 512, 1, 0, PF_R8G8B8, TU_RENDERTARGET);

		Camera* rttCam = scene_manager_->createCamera("RttCam");
		SceneNode* camNode = 
		scene_manager_->getRootSceneNode()->createChildSceneNode("rttCamNode");
		camNode->attachObject(rttCam);
		rttCam->setPosition(0,0,200);
		//rttCam->setVisible(true);
		
		Viewport *v = rttTex->getBuffer()->getRenderTarget()->addViewport( rttCam );
		v->setOverlaysEnabled(false);
		v->setClearEveryFrame( true );
		v->setBackgroundColour( ColourValue::Black );

		// Retrieve CEGUI texture for the RTT
		CEGUI::Texture* rttTexture = mRenderer->createTexture((CEGUI::utf8*)"RttTex");

		CEGUI::Imageset* rttImageSet = CEGUI::ImagesetManager::getSingleton().createImageset((CEGUI::utf8*)"RttImageset", rttTexture);

		rttImageSet->defineImage((CEGUI::utf8*)"RttImage", 
		
		CEGUI::Point(0.0f, 0.0f),
		CEGUI::Size(rttTexture->getWidth(), rttTexture->getHeight()),
		CEGUI::Point(0.0f,0.0f));
	/* end culting */

  CEGUI::ImagesetManager::getSingleton().createImageset("Vanilla.imageset");
  CEGUI::SchemeManager::getSingleton().loadScheme((CEGUI::utf8*)"VanillaSkin.scheme");

  mSystem->setDefaultMouseCursor((CEGUI::utf8*)"Vanilla-Images", (CEGUI::utf8*)"MouseArrow");
  mSystem->setDefaultFont((CEGUI::utf8*)"BlueHighway-12");

  root = CEGUI::WindowManager::getSingleton().createWindow("DefaultWindow", "Root");
  mSystem->setGUISheet(root);
}

}